#define _CRT_SECURE_NO_WARNINGS 1

#include"LinkListStack.h"

void InitStack(LinkST** s)
{
	LinkST* newstack = (LinkST*)malloc(sizeof(LinkST));
	if (newstack == NULL)
	{
		perror("newstack malloc:");
	}
	newstack->head = NULL;
	newstack->size = 0;
	*s = newstack;
}

bool StackEmpty(LinkST* s)
{
	assert(s);
	return s->size == 0;
}

void Push(LinkST* s, ElemType e)
{
	assert(s);
	LinkSTNode* newnode = (LinkSTNode*)malloc(sizeof(LinkSTNode));
	if (newnode == NULL)
	{
		perror("newnode malloc:");
		return;
	}
	newnode->data = e;
	newnode->next = s->head;
	s->head = newnode;
	s->size++;
}

bool Pop(LinkST* s, ElemType* e)
{
	assert(s);
	if (s->size == 0)
	{
		return false;
	}
	*e = s->head->data;
	LinkSTNode* cur = s->head;
	s->head = s->head->next;
	free(cur);
	cur == NULL;
	s->size--;
	return true;
}

bool GetTop(LinkST* s, ElemType* e)
{
	if (s->size == 0)
	{
		return false;
	}
	*e = s->head->data;
	return true;
}

void DestroyStack(LinkST* s)
{
	assert(s);
	LinkSTNode* cur = s->head;
	while (s->head != NULL)
	{
		s->head = s->head->next;
		free(cur);
		cur = s->head;
	}
	free(s);
}